package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.batch.IBatchAcceptService;
import com.iwhalecloud.bss.kite.cucc.client.api.batch.IBatchConfigService;
import com.iwhalecloud.bss.kite.cucc.client.dto.batch.KiteBatchAccept;
import com.iwhalecloud.bss.kite.cucc.client.dto.batch.KiteBatchAcceptDetail;
import com.iwhalecloud.bss.kite.cucc.client.dto.batch.KiteBatchConfig;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.BusiAdjustmentInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.BusiCheckResultDetailVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.BusiCheckResultFileVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.BusiCheckResultVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.BusimportDetailVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalErrorConsts;
import com.iwhalecloud.bss.kite.cucc.service.batch.enums.BatchAcceptStatusEnum;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;

/**
 * 批量受理-调整记录模块数据处理器
 * @author jojo
 *
 */
@Service
public class BusiAdjustmentModuleHandler extends AbstractModuleHandler {

    @Autowired
    private IBatchAcceptService batchAcceptService;

    @Autowired
    private IBatchConfigService localBatchAcceptTempService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        //调整记录信息模块是预览页面才有，execute直接返回空列表
        return Lists.newArrayList();
    }

    @Override
    public List<ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        BusiAdjustmentInfoVO adjustmentInfo = new BusiAdjustmentInfoVO();
        this.getCheckResult(context, sceneModule, adjustmentInfo);
        this.getImportDetails(context, sceneModule, adjustmentInfo);

        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        sceneAccountInfoVo.setBusiAdjustmentInfoVO(adjustmentInfo);
        return Collections.singletonList(adjustmentInfo);
    }

    /**
     * 获取校验结果
     * @param context
     * @param sceneModule
     * @param adjustmentInfo
     */
    private void getCheckResult(AcceptContext context, SceneModule sceneModule, BusiAdjustmentInfoVO adjustmentInfo) {
        BusiCheckResultVO checkResult = new BusiCheckResultVO();
        String batchId = this.querySceneInstService.getExtParameterValue(context.getAcceptRequest().getSceneInstId(), "batchId");
        List<KiteBatchAcceptDetail> failDetails = this.batchAcceptService.listCheckFailDetail(batchId);
        KiteBatchAccept kiteBatchAccept = this.batchAcceptService.getKiteBatchAccept(batchId);
        if (kiteBatchAccept != null) {
            List<BusiCheckResultFileVO> fileVOs = Lists.newArrayList();
            BusiCheckResultFileVO fileVO = new BusiCheckResultFileVO();
            fileVO.setId("1");
            fileVO.setBatchId(batchId);
            fileVO.setFileName(kiteBatchAccept.getFileName());
            fileVO.setStatusCd(kiteBatchAccept.getStatusCd());
            fileVO.setStatusName(BatchAcceptStatusEnum.getStatusNameByCd(kiteBatchAccept.getStatusCd()));
            fileVO.setStatusDesc(LocalErrorConsts.BSS_KITE_BATCH_50001.getMsg(kiteBatchAccept.getImportNum(), (kiteBatchAccept.getImportNum() - failDetails.size()), failDetails.size()));
            fileVOs.add(fileVO);

            checkResult.setCheckResultFiles(fileVOs);
        }

        List<BusiCheckResultDetailVO> checkResultDetails = Lists.newArrayList();
        for (int i = 0; i < failDetails.size(); i++) {
            KiteBatchAcceptDetail detail = failDetails.get(i);
            BusiCheckResultDetailVO detailVO = new BusiCheckResultDetailVO();
            detailVO.setBatchId(batchId);
            detailVO.setDetailId(detail.getDetailId().toString());
            detailVO.setLineNum(LocalErrorConsts.BSS_KITE_BATCH_50002.getMsg(detail.getLineNum()));
            detailVO.setDealDesc(detail.getCheckResult());
            detailVO.setSerialIndex(String.valueOf(i + 1));

            checkResultDetails.add(detailVO);
        }

        checkResult.setCheckResultDetails(checkResultDetails);

        adjustmentInfo.setCheckResult(checkResult);
    }

    /**
     * 获取导入明细
     * @param context
     * @param sceneModule
     * @param adjustmentInfo
     */
    private void getImportDetails(AcceptContext context, SceneModule sceneModule, BusiAdjustmentInfoVO adjustmentInfo) {

        /*String searchContent = null;
        Map<String, Object> extParams = context.getAcceptRequest().getExt();
        if (extParams != null) {
            searchContent = KiteMapUtils.getString(extParams, "searchContent");
        }
        List<BusimportDetailVO> importDetails = Lists.newArrayList();
        String batchId = this.querySceneInstService.getExtParameterValue(context.getAcceptRequest().getSceneInstId(), "batchId");
        List<KiteBatchAcceptDetail> searchImportDetails = batchAcceptService.listDetial(batchId, null, searchContent);
        KiteBatchAccept kiteBatchAccept = this.batchAcceptService.getKiteBatchAccept(batchId);
        if (KiteObjectUtils.isEmpty(kiteBatchAccept)) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("未获取到导入批次信息：" + batchId);
        }
        searchImportDetails.forEach(detail -> {
            BusimportDetailVO detailVO = new BusimportDetailVO();
            detailVO.setDetailId(detail.getDetailId().toString());
            detailVO.setOfferId(kiteBatchAccept.getOfferId());
            detailVO.setOfferName(kiteBatchAccept.getOfferName());
            Map attrMap = JSONObject.parseObject(detail.getAttrJson(), Map.class);
            detailVO.setAttrs(Collections.singletonList(attrMap));

            importDetails.add(detailVO);
        });
        adjustmentInfo.setImportDetails(importDetails);*/

        String custId = context.getAcceptRequest().getCustId();
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        this.getShowAttrs(sceneInstId, custId, adjustmentInfo);
    }

    /**
     * 获取展示列信息
     * @param sceneInstId
     * @param custId
     * @param adjustmentInfo
     */
    private void getShowAttrs(String sceneInstId, String custId, BusiAdjustmentInfoVO adjustmentInfo) {
        String rootInstId = this.querySceneInstService.getSceneInst(sceneInstId).getRootInstId();
        KiteOfferInst offerInst = this.querySceneInstService.getOfferInst(custId, rootInstId);
        String saleGoodsCode = offerInst.getAttrValue("saleGoodsCode");
        String applyRegionCode = this.querySceneInstService.getSceneInst(sceneInstId).getProvinceNbr();
        String importFileName = this.querySceneInstService.getExtParameterValue(sceneInstId, "importFileName");
        KiteBatchConfig kiteBatchConfig =
            localBatchAcceptTempService.queryConfigByOfferId(applyRegionCode, null, saleGoodsCode, importFileName);
        if (!KiteObjectUtils.isEmpty(kiteBatchConfig)) {
            String showAttrJson = kiteBatchConfig.getShowAttrJon();
            List<CommonAttrVO> commonAttrVOList = JSONObject.parseArray(showAttrJson, CommonAttrVO.class);
            commonAttrVOList.removeIf(commonAttrVO -> KiteStringUtils.equals(commonAttrVO.getIsNull(), "T"));
            adjustmentInfo.setTitleAttr(commonAttrVOList);
        }
    }
}
